package com.das.mechanic_base.adapter.maintain;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.bean.greendao.AloneServiceNewBean;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3AloneServiceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<AloneServiceNewBean> mList;

    private Context mContext;
    private final String km;
    private int afterDay;
    private X3AloneServiceSecondAdapter aloneServiceSecondAdapter;

    public X3AloneServiceAdapter(Context mContext) {
        this.mContext = mContext;
        mList = new ArrayList<>();
        km = (String) SpHelper.getData(X3Constent.MILEAGEUNIT, "km");
    }

    /**
     * 空数据时，显示空布局类型
     */
    private final int EMPTY_VIEW = 1;

    /**
     * 控制空布局的显隐
     */
    private int mEmptyType = 0;

    public void setData(List<AloneServiceNewBean> mList){
        this.mList =mList;

        if(X3StringUtils.isListEmpty(mList)){
            mEmptyType =1;
        }else {
            mEmptyType =0;
        }
        notifyDataSetChanged();
    }

    public void delItem(int position,int positionSec){
        if(!X3StringUtils.isListEmpty(mList.get(position).detectionTouchServiceEntityList) && mList.get(position).detectionTouchServiceEntityList.size() == 1){
            mList.remove(position);
            notifyItemRemoved(position);
            if(X3StringUtils.isListEmpty(mList)){
                mEmptyType =1;
            }else {
                mEmptyType =0;
            }
            notifyItemRangeChanged(position,mList.size() + 1 - position);

            return;
        }

        if(mList.get(position).adapter != null){
            mList.get(position).adapter.delItem(positionSec);
        }
    }

    public List<AloneServiceNewBean> getmList(){
        return mList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return new EmptyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_empty_record_item_text,parent,false));
        }
        return new AloneServiceHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_alone_service_system_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = getItemViewType(position);
        if (itemViewType == 0) {
            AloneServiceHolder serviceHolder = (AloneServiceHolder) holder;
            serviceHolder.tv_name.setText(mList.get(position).questionBaseDescription);
            serviceHolder.recyclerview.setLayoutManager(new LinearLayoutManager(mContext));

            aloneServiceSecondAdapter = new X3AloneServiceSecondAdapter(mContext);
            serviceHolder.recyclerview.setAdapter(aloneServiceSecondAdapter);
            mList.get(position).adapter = aloneServiceSecondAdapter;
            aloneServiceSecondAdapter.setData(mList.get(position).detectionTouchServiceEntityList,mList.get(position).questionBaseId);
            aloneServiceSecondAdapter.setiOnClickModify(new X3AloneServiceSecondAdapter.IOnClickSecondSer() {
                @Override
                public void iOnClickSecondSerDel(AloneServiceNewBean.DetectionTouchServiceEntityListBean detectionTouchServiceEntityListBean, long questionBaseId, int positionSec) {
                    if(iOnClickModify != null){
                    iOnClickModify.iOnClickModifyDel(detectionTouchServiceEntityListBean,questionBaseId,position,positionSec);
                }
                }

                @Override
                public void iOnClickItem(AloneServiceNewBean.DetectionTouchServiceEntityListBean bean,long questionBaseId) {
                    if(iOnClickModify != null){
                        iOnClickModify.iOnClickModify(bean,questionBaseId);
                    }
                }
            });


        }else {
            ViewGroup.LayoutParams layoutParams = holder.itemView.getLayoutParams();
            layoutParams.width = X3ScreenUtils.getScreenWidth(mContext);
            layoutParams.height = X3ScreenUtils.getScreenHeight(mContext);
            holder.itemView.setLayoutParams(layoutParams);
            EmptyHolder emptyHolder = (EmptyHolder) holder;
            emptyHolder.tv_empty.setText(mContext.getString(R.string.no_have_recommend));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mEmptyType == EMPTY_VIEW) {
            //空布局的类型
            return 1;
        }
        return 0;
    }

    IOnClickModify iOnClickModify;

    public void setiOnClickModify(IOnClickModify iOnClickModify) {
        this.iOnClickModify = iOnClickModify;
    }

    public interface IOnClickModify {
        void iOnClickModify(AloneServiceNewBean.DetectionTouchServiceEntityListBean bean,long questionBaseId);
        void iOnClickModifyDel(AloneServiceNewBean.DetectionTouchServiceEntityListBean detectionTouchServiceEntityListBean, long questionBaseId,int position,int positionSec);
    }

    @Override
    public int getItemCount() {
        return X3StringUtils.isListEmpty(mList) ? mEmptyType : mList.size();
    }

    class AloneServiceHolder extends RecyclerView.ViewHolder {
        RecyclerView recyclerview;
        TextView tv_name;
        public AloneServiceHolder(@NonNull View itemView) {
            super(itemView);
            recyclerview = itemView.findViewById(R.id.recyclerview);
            tv_name = itemView.findViewById(R.id.tv_name);
        }
    }

    class EmptyHolder extends RecyclerView.ViewHolder {
        TextView tv_empty;
        public EmptyHolder(@NonNull View itemView) {
            super(itemView);
            tv_empty = itemView.findViewById(R.id.tv_empty);
        }
    }
}
